/**
 *
 */
package com.mperon.util.io;

import java.io.File;
import java.io.InputStream;
import java.net.URL;

/**
 * @author Marcos Peron
 *
 */
public class FindResource {
    
    /**
     *
     */
    public static final String CLASS_EXTENSION = ".class";
    
    /**
     * @throws InstantiationError
     */
    public FindResource() throws InstantiationError {
        throw new InstantiationError("Classe estatica!!");
    }
    
    /**
     * @param classe
     * @param file
     * @return
     */
    public static String getResourceString(@SuppressWarnings("rawtypes") Class classe, String file) {
        return(FindResource.getClassDirectory(classe)+file);
        
    }
    
    /**
     *
     * @param classe
     * @param file
     * @return
     */
    @SuppressWarnings("unchecked")
    public static InputStream getResourceStream(Class classe, String file) {
        return(classe.getResourceAsStream(file));
    }
    
    /**
     * @param c Definicao da classe que se ir� buscar o resource
     * @return Diret�rio de uma classe
     */
    @SuppressWarnings("unchecked")
    public static String getClassDirectory(Class c) {
        String classFullName = c.getName();
        String className = classFullName.substring(classFullName.lastIndexOf('.')+1) + CLASS_EXTENSION;
        classFullName = null;
        URL u = c.getResource(className);
        String dir = u.getPath().substring(0, u.getPath().indexOf(className));
        return(dir);
    }
    
    /**
     *
     * @param c
     * @return
     */
    @SuppressWarnings("unchecked")
    public static File getClassDirectoryAsFile(Class c) {
        return(new File(FindResource.getClassDirectory(c)));
    }
    
}
